﻿@using Microsoft.JSInterop
@using Covenant.Models.Indicators
@using Covenant.Components.Shared
@inject IJSRuntime IJSRuntime

<CovenantTable TItem="FileIndicator" Items="FileIndicators.ToList()" SearchRow="IsRowDisplayed"
               IsSearchable="IsSearchable" IsSortable="IsSortable" IsPaginated="IsPaginated" PageLength="PageLength" class="table table-hover">
    <TableHeader>
        <SortableTableHeader TItem="FileIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.FileName">
            <HeaderContent>FileName</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="FileIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.FilePath">
            <HeaderContent>FilePath</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="FileIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.SHA2">
            <HeaderContent>SHA2</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="FileIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.SHA1">
            <HeaderContent>SHA1</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="FileIndicator" TItem2="string" GetSortableProperty="(indicator) => indicator.MD5">
            <HeaderContent>MD5</HeaderContent>
        </SortableTableHeader>
    </TableHeader>
    <TableRow Context="indicator">
        <tr>
            <td>@indicator.FileName</td>
            <td>@indicator.FilePath</td>
            <td>@indicator.SHA2</td>
            <td>@indicator.SHA1</td>
            <td>@indicator.MD5</td>
        </tr>
    </TableRow>
</CovenantTable>

@code {
    [Parameter]
    public IEnumerable<FileIndicator> FileIndicators { get; set; }

    [Parameter]
    public bool IsSearchable { get; set; } = false;

    [Parameter]
    public bool IsSortable { get; set; } = true;

    [Parameter]
    public bool IsPaginated { get; set; } = true;

    [Parameter]
    public int PageLength { get; set; } = 25;

    private bool IsRowDisplayed(FileIndicator indicator, string SearchTerm)
    {
        return SearchTerm == string.Empty ||
            indicator.FileName.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.FilePath.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.SHA2.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.SHA1.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            indicator.MD5.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase);
    }
}